package com.nf.db.handlers;

import com.nf.db.DaoException;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import static com.nf.db.handlers.RowProcessor.DEFAULT_ROWPROCESSOR;
/*
 * 让ResultSetHandler处理结果集，并将结果集转换为Object[]数组
 * */
public class ArrayHandler implements ResultSetHandler<Object[]> {
    private static final Object[] array = new Object[0];

    @Override
    public Object[] handle(ResultSet rs) {
        try {
            //判断结果集是否为空，如果为空，则返回空数组
            if (!rs.next()) return array;

            return DEFAULT_ROWPROCESSOR.toArray(rs);
        } catch (SQLException e) {
            throw new DaoException("结果集处理失败", e);
        }
    }
}
